/**
 * \file
 *           tlmRmtCfgParaNavi.cpp
 * \brief
 *           Implements tlmRmtCfgParaNavi class for Telematics services
 *           this is the class of parameters for Navi.
 *
 * \par Author (last changes):
 *          - Zeng Jie
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 * \par Project Leader:
 *          -
 * \par Responsible Developer:
 *          - Zeng Jie
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 * \par Responsible Architect:
 *          - Zhang Fa
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 *
 * \par Project:
 *      DPCA2
 *
 * \par SW-Component:
 *      Telematics
 *
 * \par SW-Package:
 *      Telematics
 *
 * \par SW-Module:
 *      Telematics Application
 *
* \par SW-Architecture Specification:
 *      DPCA2_TELEMATICS_SW_DD_07a_ready.doc
 *
* \par SW-Design Specification:
 *      DPCA2_TLM-RMTCFG_SW_DD_01d_released.doc
 *
 * \par SW-Test Case Specification:
 *
 * \par CM-Info:
 *
 * \note
 *      This is a note.
 *
 * \bug
 *      - there are no known bugs
 *
 * \par Module-History:
 *
 * \verbatim
 * Date                Author					Reason
 * 10 Dec 2013        Zeng Jie              Initial Creation
 * The latest entry at the head of the history list.
 * \endverbatim
 *
 * \par Copyright Notice:
 * Copyright (C) 2012 Continental Automotive Holdings Co., Ltd.
 * Alle Rechte vorbehalten. All Rights Reserved.\n
 * The reproduction, transmission or use of this document or its contents is
 * not permitted without express written authority.\n
 * Offenders will be liable for damages. All rights, including rights created
 * by patent grant or registration of a utility model or design, are reserved.
 *
 */

#include "stdafx.h"

#include "string.h"
#include "Winsock2.h"
#include "tlmServiceFactory.h"
#include "tlmRmtCfgParaNavi.h"
#include "teleLogTrace.h"

#include "app_IEmbRegistryIDs.h"

#ifndef _teleUMSGQ_def_h
#include "entITeleUMSGQ_def.h"
#endif

/**
* \brief tlmRmtCfgParaNavi: this is constructor of tlmRmtCfgParaNavi class
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*/
tlmRmtCfgParaNavi::tlmRmtCfgParaNavi() : tlmRmtCfgPara(TLM_RMT_CFG_PARA_NAVI_ELEMENT_COUNT),
			 mGasStationStatus(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_STATION_STATUS),
			 mGasStationUrl(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_STATION_URL,TLM_RMT_CFG_PARA_ITEM_URL_VALUE_SIZE),
			 mLocalSrchStatus(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_LOCAL_SEARCH_STATUS),
			 mLocalSrchUrl(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_LOCAL_SEARCH_URL,TLM_RMT_CFG_PARA_ITEM_URL_VALUE_SIZE),
			 mParkingStatus(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_PARKING_STATUS),
			 mParkingUrl(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_PARKING_URL,TLM_RMT_CFG_PARA_ITEM_URL_VALUE_SIZE),
			 mTrafficInfoOvFmStatus(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_TRAFFIC_INFO_OVER_FM_STATUS),
			 mTrafficInfoStatus(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_TRAFFIC_INFO_STATUS),
			 mTrafficPeriod(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_TRAFFIC_PERIOD,TLM_RMT_CFG_PARA_ITEM_PERIOD_VALUE_SIZE),
			 mTrafficUrl(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_TRAFFIC_URL,TLM_RMT_CFG_PARA_ITEM_URL_VALUE_SIZE),
			 mWeatherStatus(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_WEATHER_STATUS),
			 mWeatherUrl(TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_WEATHER_URL,TLM_RMT_CFG_PARA_ITEM_URL_VALUE_SIZE) {
    //#[ operation tlmRmtCfgParaNavi()
    
    mEleParaList[0]=&mTrafficInfoStatus;
    mEleParaList[1]=&mTrafficInfoOvFmStatus;
    mEleParaList[2]=&mTrafficPeriod;
    mEleParaList[3]=&mTrafficUrl;    
    
    mEleParaList[4]=&mLocalSrchStatus;  
    mEleParaList[5]=&mLocalSrchUrl; 
    
    mEleParaList[6]=&mWeatherStatus;
    mEleParaList[7]=&mWeatherUrl;     
    
    mEleParaList[8]=&mParkingStatus;
    mEleParaList[9]=&mParkingUrl;
    	
    
    mEleParaList[10]=&mGasStationStatus;
    mEleParaList[11]=&mGasStationUrl;

	mSrvId=TLM_RMT_CFG_SERVICE_ID_NAVI;
    	
    	
    //#]
}

/**
* \brief ~tlmRmtCfgParaNavi: this is destructor of tlmRmtCfgParaNavi class
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*/
tlmRmtCfgParaNavi::~tlmRmtCfgParaNavi() {
}



/**
* \brief GetItem : get the parameter element by it's code
*
* \note
*
* \param[in]
* - code : the code of parameters element
*
* \return
* - tlmRmtCfgElementPara*: pointer to parameter element
*
*/
tlmRmtCfgElementPara* tlmRmtCfgParaNavi::GetItem(uint8_t code) {
    //#[ operation GetItem(uint8_t)
    tlmRmtCfgElementPara * pItem=NIL;
    switch(code)
    {
    	case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_TRAFFIC_INFO_STATUS:  
        {
    		pItem = &mTrafficInfoStatus;
    		break;
        }
    	case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_TRAFFIC_INFO_OVER_FM_STATUS :   
        {
            pItem = &mTrafficInfoOvFmStatus;
    	 	break;  
        }
    	case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_TRAFFIC_PERIOD :
        {
    		pItem = &mTrafficPeriod;
    	 	break; 
        }
    	case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_TRAFFIC_URL: 
        {
    		pItem = &mTrafficUrl;
    	 	break;
        }
    	case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_LOCAL_SEARCH_STATUS:
        {
    		pItem = &mLocalSrchStatus;
    	 	break;
        }
        case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_LOCAL_SEARCH_URL:
        {
            pItem = &mLocalSrchUrl;
            break;
        }
        case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_WEATHER_STATUS:
        {
            pItem = &mWeatherStatus;
            break;
        }
    
        case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_WEATHER_URL:
        {
            pItem = &mWeatherUrl;
            break;
        }
        case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_PARKING_STATUS:
        {
            pItem = &mParkingStatus;
            break;
        }
        case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_PARKING_URL:
        {
            pItem = &mParkingUrl;
            break;
        }
        case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_STATION_STATUS:
        {
            pItem = &mGasStationStatus;
            break;
        }
        case TLM_RMT_CFG_ELEMENT_CODE_NAVI_ATT_STATION_URL:
        {
            pItem = &mGasStationUrl;
            break;
        }
    	default:
        {
            //LOG_PRINT(DPCA_TELE_MAIN_ERROR,"[TLM_MSGSRV]:<tlmRmtCfgParanavi::%s> invalid para code[%d]\n", __FUNCTION__, code);;
    		break;
        }
    }    
    
    return pItem;
    
    //#]
}

/**
* \brief Save: save NAVI parameters into NVM
*              For NAVI paramters, it's divided as
*              Traffic data
*              Local search data
*              Weather data
*              Parking data
*              Gas station data
*              if element of one section is updated
*              all elements of this section shall be saved.
*
* \note
*
* \param[in]
* - none
*
* \return
* - uint8_t: save status
*
*/
uint8_t tlmRmtCfgParaNavi::Save() {
    BOOL isUpdated = FALSE; 
    S_TLM_REMOTE_CFG_TRAFFIC sTrafficData;
    S_TLM_REMOTE_CFG_LOCAL_SEARCH sLocalSrchData;
    S_TLM_REMOTE_CFG_WEATHER sWeatherData;
    S_TLM_REMOTE_CFG_PARKING sParkingData;
    S_TLM_REMOTE_CFG_GAS_STATION sGasStationData;
    uint8_t status=0;
    uint8_t flag=0; //this flag used to indicate which filed is updated
    /*
    bit    7     6      5          4            3                2             1              0
          0   |  0  |  0   |  parking |  weather |  localsearch | traffic |  gas station
    */
     
    //check the traffic information
    //any item is updated, the total web parameters shall be save into NVM. 
    isUpdated = mTrafficInfoStatus.IsUpdated() |
    			mTrafficInfoOvFmStatus.IsUpdated() |
    			mTrafficUrl.IsUpdated() |
    			mTrafficPeriod.IsUpdated(); 
                
    if(isUpdated)
    {//the parameters is udpated  
        memset(&sTrafficData, 0, sizeof(sTrafficData));
        sTrafficData.trafficInfoStatus = *(reinterpret_cast<bool_t *>(mTrafficInfoStatus.GetValuePtr()));
        sTrafficData.trafficInfoOverFM = *(reinterpret_cast<bool_t *>(mTrafficInfoOvFmStatus.GetValuePtr())); 

        //the data in message buffer is big-endian.
        //sTrafficData.trafficPeriod = ntohs(*(reinterpret_cast<uint16_t *>(mTrafficPeriod.GetValuePtr())));
        sTrafficData.trafficPeriod = (*(reinterpret_cast<uint16_t *>(mTrafficPeriod.GetValuePtr())));
        memcpy(sTrafficData.trafficURL, mTrafficUrl.GetValuePtr(), mTrafficUrl.GetActualSize());
            
        status |= SaveParameters( ERG_DPCA_NAVI_REMOTECFG_TRAFFIC,reinterpret_cast<uint8_t*>(&sTrafficData),sizeof(sTrafficData));
        flag |= MASK_TRAFFIC;
    }      

    isUpdated=FALSE;
    //check the local search
    //any item is updated, the total web parameters shall be save into NVM. 
    isUpdated = mLocalSrchStatus.IsUpdated() | mLocalSrchUrl.IsUpdated();
                
    if(isUpdated)
    {//the parameters is udpated  
    	memset(&sLocalSrchData, 0, sizeof(sLocalSrchData));
    	sLocalSrchData.localSearchStatus = *(reinterpret_cast<bool_t *>(mLocalSrchStatus.GetValuePtr()));
    	memcpy(sLocalSrchData.localSearchURL, mLocalSrchUrl.GetValuePtr(), mLocalSrchUrl.GetActualSize());
    	    
    	status |= SaveParameters( ERG_DPCA_NAVI_REMOTECFG_LOCALSEARCH,reinterpret_cast<uint8_t*>(&sLocalSrchData),sizeof(sLocalSrchData));
      flag |= MASK_LOCAL_SEARCH;
    }   



    isUpdated=FALSE;
    //check the WeatherStatus
    //any item is updated, the total web parameters shall be save into NVM. 
    isUpdated = mWeatherStatus.IsUpdated() |
    			mWeatherUrl.IsUpdated();
                
    if(isUpdated)
    {//the parameters is udpated  
    	memset(&sWeatherData, 0, sizeof(sWeatherData));
    	sWeatherData.weatherStatus = *(reinterpret_cast<bool_t *>(mWeatherStatus.GetValuePtr()));
    	memcpy(sWeatherData.weatherURL, mWeatherUrl.GetValuePtr(), mWeatherUrl.GetActualSize());
    	    
    	status |= SaveParameters( ERG_DPCA_NAVI_REMOTECFG_WEATHER,reinterpret_cast<uint8_t*>(&sWeatherData),sizeof(sWeatherData));
        flag |= MASK_WEATHER;
    }    


    isUpdated=FALSE;
    //check the Parking
    //any item is updated, the total web parameters shall be save into NVM. 
    isUpdated = mParkingStatus.IsUpdated() | mParkingUrl.IsUpdated();
                
    if(isUpdated)
    {//the parameters is udpated  
    	memset(&sParkingData, 0, sizeof(sParkingData));
    	sParkingData.parkingStatus = *(reinterpret_cast<bool_t *>(mParkingStatus.GetValuePtr()));
    	memcpy(sParkingData.parkingURL, mParkingUrl.GetValuePtr(), mParkingUrl.GetActualSize());
    	    
    	status |= SaveParameters( ERG_DPCA_NAVI_REMOTECFG_PARKING,reinterpret_cast<uint8_t*>(&sParkingData),sizeof(sParkingData));
        flag |= MASK_PARKING;
    }  



    isUpdated=FALSE;
    //check the GasStation
    //any item is updated, the total web parameters shall be save into NVM. 
    isUpdated = mGasStationStatus.IsUpdated() | mGasStationUrl.IsUpdated();
                
    if(isUpdated)
    {//the parameters is udpated  
    	memset(&sGasStationData, 0, sizeof(sGasStationData));
    	sGasStationData.stationStatus = *(reinterpret_cast<bool_t *>(mGasStationStatus.GetValuePtr()));
    	memcpy(sGasStationData.stationURL, mGasStationUrl.GetValuePtr(), mGasStationUrl.GetActualSize());
    	    
    	status |= SaveParameters( ERG_DPCA_NAVI_REMOTECFG_GASSTATION,reinterpret_cast<uint8_t*>(&sGasStationData),sizeof(sGasStationData));
      flag |= MASK_GAS_STATION; 
    }        


    UMSGQ::getInstance().send(entTlmMsg_CFG_NAVI, &flag, 1);

    return status;

   
    
    //#]
}



/**
* \brief InitLocalSrchUrl : initialize the url of local search parameters
*
* \note
*
* \param[in]
* - url : pointer to url string buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitLocalSrchUrl(uint8_t * url) {
    if(url != NIL)
    {
        mLocalSrchUrl.InitValue(url);
    }
    
}

/**
* \brief InitLocalSrchStatus : initialize the status of local search parameters
*
* \note
*
* \param[in]
* - status : pointer to status value buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitLocalSrchStatus(uint8_t * status) {
    if(status != NIL)
    {
        mLocalSrchStatus.InitValue(status);
    }
    
}

/**
* \brief InitParkingUrl : initialize the URL of parking parameters
*
* \note
*
* \param[in]
* - url : pointer to to url string buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitParkingUrl(uint8_t * url) {
    if(url != NIL)
    {
        mParkingUrl.InitValue(url);
    }
    
}

/**
* \brief InitParkingStatus : initialize the status of parking parameters
*
* \note
*
* \param[in]
* - status : pointer to to status value buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitParkingStatus(uint8_t * status) {
    if(status != NIL)
    {
        mParkingStatus.InitValue(status);
    }
    
}

/**
* \brief InitWeatherUrl : initialize the URL of Weather parameters
*
* \note
*
* \param[in]
* - url : pointer to to url string buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitWeatherUrl(uint8_t * url) {
    if(url != NIL)
    {
        mWeatherUrl.InitValue(url);
    }
    
}

/**
* \brief InitWeatherStatus : initialize the status of weather parameters
*
* \note
*
* \param[in]
* - status : pointer to to status value buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitWeatherStatus(uint8_t * status) {
    if(status != NIL)
    {
        mWeatherStatus.InitValue(status);
    }
    
}

/**
* \brief InitGasStationUrl : initialize the URL of gas station parameters
*
* \note
*
* \param[in]
* - url : pointer to to url string buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitGasStationUrl(uint8_t * url) {
    if(url != NIL)
    {
        mGasStationUrl.InitValue(url);
    }
    
}

/**
* \brief InitGasStationStatus : initialize the status of gas station parameters
*
* \note
*
* \param[in]
* - status : pointer to to status value buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitGasStationStatus(uint8_t * status) {
    if(status != NIL)
    {
        mGasStationStatus.InitValue(status);
    }
    
}

/**
* \brief InitTrafficUrl : initialize the URL of traffic parameters
*
* \note
*
* \param[in]
* - url : pointer to to url string buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitTrafficUrl(uint8_t * url) {
    if(url != NIL)
    {
        mTrafficUrl.InitValue(url);
    }
    
}

/**
* \brief InitTrafficPeriod : initialize the period of traffic parameters
*
* \note
*
* \param[in]
* - period : pointer to to period string buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitTrafficPeriod(uint8_t * period) {
    if(period != NIL)
    {
        mTrafficUrl.InitValue(period);
    }
    
}

/**
* \brief InitTrafficFmStatus : initialize the FM status of traffic parameters
*
* \note
*
* \param[in]
* - fmStatus : pointer to to FM status string buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitTrafficFmStatus(uint8_t * fmStatus) {
    if(fmStatus != NIL)
    {
        mTrafficInfoOvFmStatus.InitValue(fmStatus);
    }
    
}

/**
* \brief InitTrafficInfoStatus : initialize the infomation status of traffic parameters
*
* \note
*
* \param[in]
* - infostatus : pointer to to info status string buffer
*
* \return 
* - none
*
*/
void tlmRmtCfgParaNavi::InitTrafficInfoStatus(uint8_t * infoStatus) {
    if(infoStatus != NIL)
    {
        mTrafficInfoStatus.InitValue(infoStatus);
    }
    
}

